package googleearthdownloader;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.lang.reflect.InvocationTargetException;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;


public class Main{
	
	// Variables
	private MapViewerSystem mapViewerSystem;
	private ProxySystem proxySystem;
	private DownloadSystem downloadSystem;
	private SystemLog systemLog;
	private JWindow startupScreen;
	private JProgressBar startupProgressBar;
	private JLabel startupLabel;
	private JPanel jp;
	private int screenHeight,screenWidth, logLevel;
	
	public Main(){
		
		
		//TODO setFlags
		
		int vValue = 145 ; // "v=" version number. must be set manually regularly from tiles in Browser
		boolean useproxy = false; // do you want to use the proxy system or connect directly, you may get blocked by google when not using proxys
		boolean downloadNewProxys = false;
		int downloadthreads = 40;
		int downloadtimeout = 5000;
		
		logLevel = SystemLog.LOG_FULL;
		//logLevel = SystemLog.LOG_OFF;
		
		
		
		// possible tiles are 2^ZOOM * 2^Zoom
		// adresses from 0 to 2^ZOOM -1
		// level 15 flip to side View in Stuttgart
		
		//Korsika
		/*xstart 	= 68648;
		xend 	= 68894;
		ystart 	= 48147;
		yend 	= 48964;
		z		= 17;*/
		
		//Mallorca
		/*
		xstart 	= 140835;
		xend 	= 140850;
		ystart 	= 85984;
		yend 	= 86000;
		z		= 18;*/
		
		showStartupScreen();
		
		mapViewerSystem = new MapViewerSystem(systemLog);
		systemLog = new SystemLog(mapViewerSystem.getMainFrame(),logLevel, false, false);
		proxySystem = new ProxySystem("socksProxy.txt",systemLog, downloadNewProxys);
		downloadSystem = new DownloadSystem(proxySystem,mapViewerSystem.getMainFrame(),systemLog,vValue,useproxy,downloadthreads,downloadtimeout);
		
		closeStartupScreen();
	}
	
	private void closeStartupScreen(){
		SwingUtilities.invokeLater(new Runnable(){

			@Override
			public void run() {
				
				startupProgressBar.setIndeterminate(false);
				startupScreen.setVisible(false);
				startupScreen.dispose();
			}
		});
		
		systemLog.log("system ready!",SystemLog.INFO_MESSAGE);
	}
	
	private void showStartupScreen(){
		try {
			SwingUtilities.invokeAndWait(new Runnable(){

				@Override
				public void run() {
					screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
					screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
					
					startupScreen = new JWindow();
					
					jp = new JPanel(new BorderLayout());
					jp.setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
					jp.setBackground(Color.WHITE);
			
					startupProgressBar= new JProgressBar();
					startupProgressBar.setPreferredSize(new Dimension(screenWidth/4,screenHeight/8));
					startupProgressBar.setBackground(Color.WHITE);
					
					startupLabel = new JLabel();
					startupLabel.setOpaque(false);
					startupLabel.setBackground(Color.WHITE);
					startupLabel.setPreferredSize(new Dimension(screenWidth/4,screenHeight/8));
					startupLabel.setHorizontalAlignment(JLabel.CENTER);
					startupLabel.setFont(new Font("Verdana",Font.PLAIN,screenHeight/22));
					startupLabel.setText("starting system");
					
					jp.add(startupLabel,BorderLayout.NORTH);
					jp.add(startupProgressBar,BorderLayout.SOUTH);
					startupScreen.add(jp);
					startupScreen.pack();
					startupScreen.setLocation((screenWidth-startupScreen.getWidth() ) / 2,(screenHeight - startupScreen.getHeight()) / 2 );
					startupScreen.setVisible(true);
					startupProgressBar.setIndeterminate(true);
					
				}
				
			});
		} catch (InterruptedException e) {
			System.out.println("InterruptedException in startupScreen creation");
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			System.out.println("InvocationTargetException in startupScreen creation");
			e.printStackTrace();
		}
	}
	
	
	public static void main(String[] args) {
		new Main();
	}
}
